/**
 *
 */
package com.newer.utils;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.newer.commons.annotation.sql.BetweenCondition;
import com.newer.commons.annotation.sql.InCondition;
import com.newer.commons.annotation.sql.LikeCondition;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 数据库条件查询转换工具
 *
 * @author xuntj
 * @create 2018-04-04 9:40
 **/
public class TableQueryConvertUtils {


    /**
     * 日志信息打印
     */
    static Logger logger = LogManager.getLogger(TableQueryConvertUtils.class);

    /**
     * mapper条件转换类
     *
     * @param target
     * @return
     */
    public static EntityWrapper convertEntity(Object target) {
        // 获取实体转换类
        EntityWrapper ew = new EntityWrapper();
        // 获取所有field
        Field[] fields = target.getClass().getDeclaredFields();
        // 循环所有的field
        for (Field field : fields) {
            // 设置可以访问
            field.setAccessible(true);
            // 通过field获取值
            try {
                // 获取值
                Object value = field.get(target);
                // 判断值是否为空
                if (value == null) {
                    // 继续下一个转换
                    continue;
                }
                TableField annotation = null;
                // 判断字段被那个注解修饰
                if (field.isAnnotationPresent(TableField.class)) {
                    annotation = field.getAnnotation(TableField.class);
                }
                // 被like修饰
                if (field.isAnnotationPresent(LikeCondition.class)) {
                    if (annotation != null) {
                        ew.like(annotation.value(), String.valueOf(value));
                    } else {
                        ew.like(field.getName(), String.valueOf(value));
                    }
                    continue;
                }
                // 被between修饰
                if (value instanceof List && field.isAnnotationPresent(BetweenCondition.class)) {
                    List list = (List) value;
                    if (annotation != null) {
                        ew.between(annotation.value(), list.get(0), list.get(1));
                    } else {
                        ew.between(field.getName(), list.get(0), list.get(1));
                    }
                    continue;
                }
                // 被in修饰
                if (value instanceof List && field.isAnnotationPresent(InCondition.class)) {
                    List list = (List) value;
                    if (annotation != null) {
                        ew.in(annotation.value(), list);
                    } else {
                        ew.in(field.getName(), list);
                    }
                    continue;
                }
                // 判断是否为空
                if (annotation != null) {
                    ew.eq(annotation.value(), value);
                } else {
                    ew.eq(field.getName(), value);
                }
            } catch (Exception e) {
                logger.error("转换查询对象字段失败,失败原因:" + e.getCause(), e);
            }
        }
        return ew;
    }
}
